
	// PATRON CLIENT RELATIONSHIPS  (p1_e7m, p3_b, p2_b, p1_e12m, p2_e2w2w, p3_e5w)

		tab1  ptrnjob_e  ptrnbiz_e ptrnfood_e ptrnschl_e ptrnhsing_e ptrncmder_e ptrnblkmkt_e ptrndrugsell_e ptrn4wamt_e  ptrn4wamtunit_e ptrneveramt_e ptrneveramtunit_e
		sum   ptrnjob_e  ptrnbiz_e ptrnfood_e ptrnschl_e ptrnhsing_e ptrncmder_e ptrnblkmkt_e ptrndrugsell_e ptrn4wamt_e  ptrn4wamtunit_e ptrneveramt_e ptrneveramtunit_e

		* Construct Dummies
		gen patrondum_e = (ptrnjob_e ==1  | ptrnbiz_e  ==1  |ptrnfood_e ==1  | ptrnschl_e ==1  | ptrnhsing_e ==1) if p1_e5m ==1 | p2_e2w2w ==1 | p1_e12m==1 | p1_e7m ==1 |p3_e5w==1 | p2_e12m==1  |p1_e10y==1 |p2_e10y==1 |p3_e10y==1
			la var patrondum_e "PC: has a patron"

		gen patronbaddum_e = (patrondum_e ==1 & (ptrncmder_e==1 | ptrnblkmkt_e==1 | ptrndrugsell_e==1) ) if p1_e5m ==1 | p2_e2w2w ==1 | p1_e12m==1 | p1_e7m ==1 |p3_e5w==1 | p2_e12m==1  |p1_e10y==1 |p2_e10y==1 |p3_e10y==1
			la var patronbaddum_e "PC: has a patron involved in drugs black deed commander"

		gen patrondum_b = (ptrnjob_b  ==1  | ptrnbiz_b   ==1  |ptrnfood_b  ==1  | ptrnschl_b ==1  | ptrnhsing_b ==1) if p2==1 | p3==1  |p1_e10y==1 |p2_e10y==1 |p3_e10y==1
			la var patrondum_b "PC: has a patron"

		gen patronbaddum_b = (patrondum_b ==1 & (ptrncmder_b==1 | ptrnblkmkt_b==1 | ptrndrugsell_b==1) ) if p2==1 | p3==1  |p1_e10y==1 |p2_e10y==1 |p3_e10y==1
			la var patronbaddum_b "PC: has a patron involved in drugs black deed commander"

		egen patronlvl_e = rowtotal(ptrnjob_e ptrnbiz_e ptrnfood_e ptrnschl_e  ptrnhsing_e) if p1_e5m ==1 | p2_e2w2w ==1 | p1_e12m==1 | p1_e7m ==1 |p3_e5w==1 | p2_e12m==1  |p1_e10y==1 |p2_e10y==1 |p3_e10y==1
			qui sum patronlvl_e
			qui replace patronlvl_e = (patronlvl_e-r(mean))/r(sd)
			la var patronlvl_e "PC: total patron support, z"

		* 3 people answered the value questions but said that they don't have patrons we replace by missing
			replace ptrn4wamt_e=. if patrondum_e==0
			replace ptrneveramt_e=. if patrondum_e==0

			replace ptrn4wamt_e = 0 if patrondum_e==0
			replace ptrneveramt_e = 0 if patrondum_e==0



** PEER QUALITY **

	// PEER QUALITY INDEX

		* Endline  (p2_e2w and p1_e7m, p1_e13m, p3_e2w)

			gen peerquality_e = 0 if p2_e2wks==1 | p1_e7m==1 | p1_e13m==1 | p3_e2w==1 | p3_e13m==1 | p2_e13m==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1
				la var peerquality_e "Peer Quality Index (-10 10) -1 if 1 to neg question and +1 if 1 to pos question"

			* Positive
			foreach x in peerschool_e peercommmtg_e peerchurch_e peerwork_e peerbusiness_e peersave_e peertrust_e peershare_e peercheer_e peerguard_e {
				replace peerquality_e = peerquality_e + 1 if `x'==1
			}

			* Negative
			foreach x in peerbeg_e peerdrunk_e peerdrugs_e peerpickpocket_e peerburglary_e  peerrobbers_e peergamble_e peerexcom_e peercomdr_e peersmallpal_e peerbigpal_e {
				replace peerquality_e = peerquality_e - 1 if `x'==1
			}

			* Create correct-pointing variable for table *
			foreach x in peerbeg peerdrunk peerdrugs peerpickpocket peerburglary  peerrobbers peergamble peerexcom peercomdr peersmallpal peerbigpal {
			gen `x'_resc_e = -`x'_e + 1
			   cap label var `x'_resc_e `"Resc: `: variable label `x'_e '"'
			}

		* Baseline (p2_b and p1_b)

			gen peerquality_b = 0 if p2==1 | p1==1

			* PHASE 2

				* Positive
				foreach x in peerschool_b peercommmtg_b peerchurch_b peerwork_b peerbusiness_b peersave_b peertrust_b peershare_b peercheer_b peerguard_b {
					replace peerquality_b = peerquality_b +1 if `x'==1 & p2 ==1
				}

				* Negative
				foreach x in peerbeg_b peerdrunk_b peerdrugs_b peerpickpocket_b peerburglary_b  peerrobbers_b peergamble_b peerexcom_b peercomdr_b peersmallpal_b peerbigpal_b {
					replace peerquality_b = peerquality_b - 1 if `x'==1 & p2 ==1
				}

			* PHASE 1

				* In phase 1 Baseline, slightly different version.
					* peerrobbers_b is not in the p1_b
					* peeradvice is in p1_b only
					* peerpalava_b is in p1 and in p2 we have 2 questions: peerbigpal_b and  peersmallpal_b
				* In phase 1, there are lots of missing values.
				mdesc  peerschool_b peercommmtg_b peerchurch_b peerwork_b peerbeg_b peerbusiness_b peerdrunk_b peerdrugs_b peersave_b peertrust_b peerpickpocket_b peerburglary_b   peergamble_b peerexcom_b peercomdr_b peershare_b  peerpalava_b peercheer_b peerguard_b peeradvice_b if surveyed_p1_r1_b==1
				tab1 peerschool_b peercommmtg_b peerchurch_b peerwork_b peerbeg_b peerbusiness_b peerdrunk_b peerdrugs_b peersave_b peertrust_b peerpickpocket_b peerburglary_b   peergamble_b peerexcom_b peercomdr_b peershare_b  peerpalava_b peercheer_b peerguard_b peeradvice_b if surveyed_p1_r1_b==1, mi

				* Count missing values
					gen peer_missing_b = 0 if p1 ==1
					foreach x in peerschool_b peercommmtg_b peerchurch_b peerwork_b peerbeg_b peerbusiness_b peerdrunk_b peerdrugs_b peersave_b peertrust_b peerpickpocket_b peerburglary_b   peergamble_b peerexcom_b peercomdr_b peershare_b  peerpalava_b peercheer_b peerguard_b peeradvice_b {
						replace peer_missing_b = peer_missing_b +1 if `x'==. & p1 ==1
					}
					ta peer_missing_b if surveyed_p1_r1_b==1

				* Positive
				foreach x in peerschool_b peercommmtg_b peerchurch_b peerwork_b peerbusiness_b peersave_b peertrust_b peershare_b peercheer_b peerguard_b peeradvice_b{
					replace peerquality_b = peerquality_b +1 if `x'==1 & p1 ==1
				}

				* Negative
				foreach x in peerbeg_b peerdrunk_b peerdrugs_b peerpickpocket_b peerburglary_b  peergamble_b peerexcom_b peercomdr_b peerpalava_b {
					replace peerquality_b = peerquality_b - 1 if `x'==1 & p1 ==1
				}
		* Baseline (p3_b)
		sum peerquality_b if phase == 1 | phase == 2, d

		replace peerquality_b = r(p50) if p3==1

		sum peerquality_b if surveyed_p2_r1_b==1, d
		gen peerqualitydum_b = (peerquality_b>=r(p50)) if p2==1

		sum peerquality_b if surveyed_p1_r1_b==1, d
		replace peerqualitydum_b = (peerquality_b>=r(p50)) if p1==1

	// CONNECTION TO COMMANDERS

		* ENDLINE (p1_e7m,  p1_e13m)

			* Cleaning for talkcomm_e
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m
				replace talkcomm_e=2011 - talkcomm_e if p1_e7m


               replace talkcomm_e=2011 - talkcomm_e if p1_e7m
               replace talkcomm_e=2011 - talkcomm_e if p1_e7m
               replace talkcomm_e=2011 - talkcomm_e if p1_e7m
               replace talkcomm_e=2011 - talkcomm_e if p1_e7m
               replace talkcomm_e=2011 - talkcomm_e if p1_e7m
               replace talkcomm_e=2011 - talkcomm_e if p1_e7m



			* Replace missing by zero for the skip pattern
				replace fightwarunit_e = 0 if fexcoms_e==1

			* Look at the variables
				tab1 peercomdr_e fexcoms_e fightwarunit_e friendspeakwar_e  relcomm_e jobcomm_e repcomm_e talkcomm_e talkcommunit_e
				d peercomdr_e fexcoms_e fightwarunit_e friendspeakwar_e  relcomm_e jobcomm_e repcomm_e talkcomm_e talkcommunit_e

			* Dummy
				gen excomfriends_e = (fexcoms_e!=1 & fexcoms_e!=.d) if fexcoms_e!=.
				replace excomfriends_e =1 if friendspeakwar_e==3 & fexcoms_e==.d
					la var excomfriends_e "1 if is friend with at least one ex combattant"

				* This is 0 if they don't have friends.
				gen excomfriendstalkwar_e = (friendspeakwar_e!=4) if  friendspeakwar_e!=. & friendspeakwar_e!=.d
				replace excomfriendstalkwar_e =0 if fexcoms_e==1
					la var excomfriendstalkwar_e "1 if is friend with ex combattant"


			  *Massive imputation of zeros here.
				egen rel_commanders_e = rowtotal(peercomdr_e relcomm_e jobcomm_e repcomm_e), m
				replace  rel_commanders_e = 0 if (round==8) & excomfriends_e ==0 &  missing(rel_commanders_e)
					qui sum rel_commanders_e
					qui replace rel_commanders_e = (rel_commanders_e-r(mean))/r(sd)
					la var rel_commanders_e "Index of close relationships with commanders, Z"


				egen rel_commanders_old_e = rowtotal(peercomdr_e relcomm_e jobcomm_e repcomm_e)
				replace  rel_commanders_old_e = 0 if (round==8) & excomfriends_e ==0 &  missing(rel_commanders_old_e)
					qui sum rel_commanders_old_e
					qui replace rel_commanders_old_e = (rel_commanders_old_e-r(mean))/r(sd)
					la var rel_commanders_old_e "Index of close relationships with commanders, Z (old)"


/*

fsum peercomdr_e relcomm_e jobcomm_e repcomm_e if round == 8
fsum rel_commanders_tyav if round == 7
*/


		* BASELINE (p1_b, p2_b, p3_b)

			* Cleaning for talkcomm_b
				replace talkcomm_b=2011-2004 if talkcomm_b ==2004 & p2==1
				replace talkcomm_b=2011-2006 if talkcomm_b ==2006 & p2==1

			* Replace missing by zero for the skip pattern
				replace fightwarunit_b = 0 if fexcoms_b==1 & (p2==1 | p3==1)

			* in p3 and p2, same version as in endline.
				* In p1 we only have relcomm_b
				tab1 relcomm_b  if surveyed_p1_r1_b ==1
				tab1 peercomdr_b fexcoms_b fightwarunit_b friendspeakwar_b relcomm_b jobcomm_b repcomm_b talkcomm_b talkcommunit_b if surveyed_p2_r1_b ==1
				tab1 peercomdr_b fexcoms_b fightwarunit_b friendspeakwar_b relcomm_b jobcomm_b repcomm_b talkcomm_b talkcommunit_b if p3 ==1

			* Dummy
				gen excomfriends_b = (fexcoms_b!=1 & fexcoms_b!=.d) if (p2==1 | p3 ==1) & fexcoms_b!=.
				replace excomfriends_b =1 if friendspeakwar_b==2 & fexcoms_b==.d
				replace excomfriends_b =1 if friendspeakwar_b==2 & fexcoms_b==.
					la var excomfriends_b "1 if is friend with at least one ex combattant"

			* This is 0 if they don't have friends.
				gen excomfriendstalkwar_b = (friendspeakwar_b!=4) if (p2==1 | p3 ==1) & friendspeakwar_b!=. & friendspeakwar_b!=.d
				replace excomfriendstalkwar_b =0 if fexcoms_b==1
					la var excomfriendstalkwar_b "1 if is friend with ex combattant"

				egen rel_commanders_b = rowtotal(peercomdr_b relcomm_b jobcomm_b repcomm_b) if p1 ==1 | p2 ==1 | p3 ==1
					la var rel_commanders_b "Index of close relationships with commanders"

		* INDEX OF WAR EXPERIENCE
			egen warexper_b = rowtotal(bullets_b attackcutlass_b beat_b kill_b battles_b beating_b battleinj_b uviol_b regret_b thoughtdie_b refugee_b displaced_b), m
				la var warexper_b "Sum of 12 dummies for various war experiences"

			summ warexper_b
			gen warexper_z_b = (warexper_b-r(mean))/r(sd)
			la var warexper_z_b "Index of war experiences"

	// FAMILY SUPPORT

		foreach x in famseeoften famconcern famadvise famhelp {
					gen `x'_resc_e = `x'_e * (-1) + 4
					local lab: var lab `x'_e
					la var `x'_resc_e "`lab' (0-3) ADD MORE SUPPORT"
				}

		egen famsupportindex_e=rowtotal(famseeoften_resc_e famconcern_resc_e famadvise_resc_e famhelp_resc_e), m
			qui sum famsupportindex_e
			replace famsupportindex_e = (famsupportindex_e-r(mean))/r(sd)
			la var famsupportindex_e "Family: Family Support Index, Z"
